package com.example.ajouconqueror;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.example.classes.IP;
import com.example.classes.UserApplication;
import com.google.gson.Gson;

public class RequestService extends Service implements Runnable {
	Thread t = null;
	IP ip = new IP();
	private final String HOST = ip.ip;
	private final int PORT = 8080;
	private final String STATE_POST = "/AjouConquerorServer-war/webresources/ForGameOver";
	private static final String TAG = "AC";
	UserApplication UserApp;
	boolean flag = true;

	@Override
	public void onCreate() {
		super.onCreate();
		UserApp = (UserApplication) getApplication();
		t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {
		HttpHost target = new HttpHost(HOST, PORT);
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(STATE_POST);

		// set the MIME type for POST message body.
		post.setHeader("Content-Type", "application/json");
		String str = StringtoJSON(UserApp.getRoomid()+"");
		Log.d(TAG, UserApp.getRoomid()+"");

		HttpEntity entity = null;
		StringEntity se;
		while (flag) {
			try {
				se = new StringEntity(UserApp.getRoomid()+"", HTTP.UTF_8);
				post.setEntity(se);
				HttpResponse response = client.execute(target, post);
				Log.d(TAG, "in Gameover: "
						+ response.getStatusLine().getStatusCode());

				if (response.getStatusLine().getStatusCode() == 200) {
					entity = response.getEntity();
					str = EntityUtils.toString(entity, "utf-8");
					if (JSONtoString(str) == "true") flag = false;
					else flag = true;
				}
				Thread.sleep(3000);

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Intent intent = new Intent("com.example.ajouconqueror.GAMEOVER");
		sendBroadcast(intent);
	}
	
	private String StringtoJSON(String roomid) {
		Gson gson = new Gson();
		String json = gson.toJson(roomid);
		return json;
	}
	
	private String JSONtoString(String json) {
		Gson gson = new Gson();
		String s = gson.fromJson(json, String.class);
		return s;
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
